package com.manli.api.service.wechat;

import com.manli.api.dao.UserDao;
import com.manli.api.dao.WeChatUserInfoDetailDao;
import com.manli.api.dao.wechat.WeChatUserInfoDao;
import com.manli.api.dto.User;
import com.manli.api.dto.UserLinkUnionid;
import com.manli.api.dto.WeChatUserInfoDetail;
import com.manli.api.service.UserLinkUnionidService;
import com.manli.api.service.WeChatUserInfoDetailService;
import com.manli.wechat.pojo.WeChatUserInfo;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;


@Service("weChatUserInfoService")
@Transactional
public class WeChatUserInfoService {
    @Autowired
    WeChatUserInfoDao weChatUserInfoDao;
    @Autowired
    SQLManager sqlManager;
    @Autowired
    UserLinkUnionidService userLinkUnionidService;
    @Autowired
    WeChatUserInfoDetailDao weChatUserInfoDetailDao;
    @Autowired
    UserDao userDao;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from weChatUserInfo where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<WeChatUserInfo> pageQuery = new PageQuery<WeChatUserInfo>(params.getPage(),params.getLimit());
        PageQuery<WeChatUserInfo> query =sqlManager.execute(sqlReady,WeChatUserInfo.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<WeChatUserInfo> list = weChatUserInfoDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer weChatUserInfoId) {
        WeChatUserInfo weChatUserInfo = weChatUserInfoDao.single(weChatUserInfoId);
        return ResultUtil.success(weChatUserInfo);
    }

    public WeChatUserInfo getByUnionidOrOpenid(String unionid,String openid) {
        WeChatUserInfo retWeChatUserInfo = weChatUserInfoDao.createLambdaQuery()
                .andEq("openid",openid).single();
        if(retWeChatUserInfo == null){
            return null ;
        }
        if ( unionid == null ){
            unionid = "";
        }else{
            //查询获取allOpenId
            SQLReady sqlReady = new SQLReady("" +
                    "SELECT GROUP_CONCAT(openid) as allOpenId " +
                    "FROM  `we_chat_user_info` " +
                    "where unionid = ? " +
                    "group by unionid ",unionid);
            String allOpenId = sqlManager.execute(sqlReady,String.class).get(0);
            retWeChatUserInfo.setAllOpenId(allOpenId);
            long hasCountWechat = weChatUserInfoDao.createLambdaQuery().appendSql("where 1=1 and unionid='"+ unionid +"' and source=1 ").count();
            if(hasCountWechat != 0 ){
                retWeChatUserInfo.setSubscribe("1");
            }
            //查询商户职员Id
            UserLinkUnionid userLinkUnionid = userLinkUnionidService.getSingleByUnionid(unionid,1);
            if(userLinkUnionid != null){
                User user = userDao.single(userLinkUnionid.getUserId());
                retWeChatUserInfo.setStoreClerkUserId(userLinkUnionid.getUserId());
                retWeChatUserInfo.setStoreClerkBranchId(user.getBranchId());
                retWeChatUserInfo.setStoreClerkPartnerId(user.getPartnerId());
            }
            //设置绑定手机号
            WeChatUserInfoDetail weChatUserInfoDetail = weChatUserInfoDetailDao.createLambdaQuery().andEq("unionid",unionid).single();
            if(weChatUserInfoDetail != null){
                retWeChatUserInfo.setPhoneNum(weChatUserInfoDetail.getPhoneNum());
            }
        }
        return  retWeChatUserInfo;
    }


    public WeChatUserInfo save(WeChatUserInfo weChatUserInfo) {
        Integer id = weChatUserInfoDao.insertReturnKey(weChatUserInfo).getInt();
        weChatUserInfo.setWeChatUserInfoId(id);
        return weChatUserInfo;
    }


    public Result updateById(WeChatUserInfo weChatUserInfo) {
        weChatUserInfoDao.updateTemplateById(weChatUserInfo);
        return ResultUtil.success();
    }


	public Result updateTemplateById(WeChatUserInfo weChatUserInfo) {
        weChatUserInfoDao.updateTemplateById(weChatUserInfo);
        return ResultUtil.success();
    }



}
